#set(tableComment = table.getComment())
#set(primaryKeyType = table.getPrimaryKey().getPropertySimpleType())
#set(entityClassName = table.buildEntityClassName())
#set(entityVarName = firstCharToLowerCase(entityClassName))
#set(tableDefName = entityClassName.replaceAll("([a-z])([A-Z])", "$1_$2").toUpperCase())
package #(customConfig.daoPackage);

import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryChain;
import com.mybatisflex.core.query.QueryMethods;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import #(packageConfig.entityPackage).#(table.buildEntityClassName());
import #(packageConfig.mapperPackage).#(table.buildMapperClassName());
import #(packageConfig.basePackage).dto.#(entityClassName)PageQueryDTO;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import static #(packageConfig.entityPackage).table.#(entityClassName)TableDef.#(tableDefName);

/**
 * #(table.getComment()) 数据服务层。
 *
 * @author #(javadocConfig.getAuthor())
 */
@Service
public class #(customConfig.daoClassName) extends ServiceImpl<#(table.buildMapperClassName()), #(table.buildEntityClassName())>{

    /**
     * 分页查询#(tableComment)。
     *
     * @param #(entityVarName)PageQueryDTO 分页参数对象
     */
    public Page<#(entityClassName)> page(#(entityClassName)PageQueryDTO #(entityVarName)PageQueryDTO) {
        Page<#(entityClassName)> page = new Page<>(#(entityVarName)PageQueryDTO.getPageNumber(), #(entityVarName)PageQueryDTO.getPageSize());
        QueryChain<#(entityClassName)> queryChain = queryChain();

        // 根据DTO的字段值动态构建查询条件
#for(column : table.columns)
#if(column.property != "createTime" && column.property != "updateTime" && column.property != "isDeleted" && column.property != "version" && column.property != table.getPrimaryKey().property)
#set(columnName = column.property.replaceAll("([a-z])([A-Z])", "$1_$2").toUpperCase())
#if(column.propertySimpleType == "String")
        // String类型使用Like查询
        if (StringUtils.hasText(#(entityVarName)PageQueryDTO.get#(firstCharToUpperCase(column.property))())) {
            queryChain.where(#(tableDefName).#(columnName).like(#(entityVarName)PageQueryDTO.get#(firstCharToUpperCase(column.property))()));
        }
#elseif(column.propertySimpleType == "Date")
        // Date类型使用Between查询
        if (#(entityVarName)PageQueryDTO.get#(firstCharToUpperCase(column.property))Start() != null) {
            queryChain.where(#(tableDefName).#(columnName).ge(#(entityVarName)PageQueryDTO.get#(firstCharToUpperCase(column.property))Start()));
        }
        if (#(entityVarName)PageQueryDTO.get#(firstCharToUpperCase(column.property))End() != null) {
            queryChain.where(#(tableDefName).#(columnName).le(#(entityVarName)PageQueryDTO.get#(firstCharToUpperCase(column.property))End()));
        }
#else
        // 其他基本类型使用Eq查询
        if (#(entityVarName)PageQueryDTO.get#(firstCharToUpperCase(column.property))() != null) {
            queryChain.where(#(tableDefName).#(columnName).eq(#(entityVarName)PageQueryDTO.get#(firstCharToUpperCase(column.property))()));
        }
#end
#end
#end

        // 时间字段特殊处理
        // createTime - 大于等于指定时间
        if (#(entityVarName)PageQueryDTO.getCreateTime() != null) {
            queryChain.where(#(tableDefName).CREATE_TIME.ge(#(entityVarName)PageQueryDTO.getCreateTime()));
        }

        // updateTime - 小于等于指定时间
        if (#(entityVarName)PageQueryDTO.getUpdateTime() != null) {
            queryChain.where(#(tableDefName).UPDATE_TIME.le(#(entityVarName)PageQueryDTO.getUpdateTime()));
        }

        return queryChain.page(page);
    }
}